package com.wanlian.dao.impl;

import com.wanlian.dao.DeviceDao;
import com.wanlian.dao.StorageDao;
import com.wanlian.entity.Device;
import com.wanlian.entity.Storage;
import com.wanlian.error.SuccessCode;
import com.wanlian.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class StorageDaoImpl extends GenericDaoImpl<Storage> implements StorageDao {

    @Autowired
    private DeviceDao deviceDao;

    public StorageDaoImpl() {
        super(Storage.class);
    }


    /**
     * 通过userid获取用户云盘
     *
     * @param userId
     * @return
     */
    @Override
    public Storage queryByUserId(int userId, String deviceId) {
        String hql = String.format("from %s where userId=? and deviceid=? and isEnable=?", this.entityClassName());
        return this.queryForObject(hql, new Object[]{userId, deviceId, true});
    }


    @Override
    public Storage queryByDeviceId(String deviceId) {
        String hql = String.format("from %s where  deviceid=? and isEnable=?", this.entityClassName());
        return this.queryForObject(hql, new Object[]{deviceId, true});
    }

    @Override
    public Storage queryByDeviceKey(String deviceKey) {
        Device device = deviceDao.queryByDeviceKey(deviceKey);
        String hql = String.format("from %s where  deviceid=? and isEnable=?", this.entityClassName());
        return this.queryForObject(hql, new Object[]{device.getDeviceId(), true});
    }
}
